Completed
Push — master ( 430c18...52d1c6 )
by
unknown
54s
created

generate-posts.js ➔ publishNext   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 3
c 3
b 0
f 1
nc 2
dl 0
loc 51
rs 9.4109
nop 4

3 Functions

Rating   Name   Duplication   Size   Complexity  
B generate-posts.js ➔ ... ➔ ??? 0 33 3
A generate-posts.js ➔ ... ➔ p.catch 0 4 1
A generate-posts.js ➔ ... ➔ p.then 0 3 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import path from 'path'
2
import fse from 'fs-extra'
3
import {init, log, trace, error, processConfig, getTime} from './initAbeForProcesses'
4
5
// IMPORT LIB
6
var Page = require('../../cli').Page
7
var cmsOperations = require('../../cli').cmsOperations
8
var abeExtend = require('../../cli').abeExtend
9
var Manager = require('../../cli').Manager
10
var cmsData = require('../../cli').cmsData
11
var cmsTemplates = require('../../cli').cmsTemplates
12
var config = require('../../cli').config
13
var templatesTexts = {}
14
15
function publishNext(files, tt, cb, i = 0) {
16
  var pub = files.shift()
17
  if(typeof pub !== 'undefined' && pub !== null) {
18
    
19
    var jsonObject = fse.readJsonSync(pub[processConfig.ABE_STATUS].path)
20
    i++
21
    var p = new Promise((resolve) => {
22
      if(typeof templatesTexts[jsonObject.abe_meta.template] === 'undefined' || templatesTexts[jsonObject.abe_meta.template] === null) {
23
        templatesTexts[jsonObject.abe_meta.template] = cmsTemplates.template.getTemplate(jsonObject.abe_meta.template)
24
      }
25
26
      cmsData.source.getDataList(path.dirname(jsonObject.abe_meta.link), templatesTexts[jsonObject.abe_meta.template], jsonObject, true)
27
        .then(() => {
28
          jsonObject = abeExtend.hooks.instance.trigger('afterGetDataListOnSave', jsonObject)
29
30
          var obj = {
31
            type: jsonObject.abe_meta.status,
32
            json: {
33
              content: jsonObject
34
            }
35
          }
36
37
          var page = new Page(obj.json.content.abe_meta.template, templatesTexts[jsonObject.abe_meta.template], obj.json.content, true)
38
39
          cmsOperations.save.saveHtml(
40
            path.join(config.root, processConfig.ABE_DESTINATION, jsonObject.abe_meta.link),
41
            page.html
42
          )
43
          
44
          obj = abeExtend.hooks.instance.trigger('afterSave', obj)
0 ignored issues
show
Unused Code introduced by
The assignment to variable obj seems to be never used. Consider removing it.
Loading history...
45
46
          trace('('+ getTime() + ') ' + i + ' - ' + pub[processConfig.ABE_STATUS].path.replace(config.root, '').replace(config.data.url, '') + ' (tpl: ' + jsonObject.abe_meta.template + ')')
47
          resolve()
48
        },
49
        () => {
50
          error('generate-posts ERROR on ' + pub[processConfig.ABE_STATUS].path.replace(config.root, '').replace(config.data.url, ''))
51
          resolve()
52
        })
53
    })
54
  
55
    p.then(function () {
56
      publishNext(files, tt, cb, i++)
57
    })
58
    .catch(function (e) {
59
      publishNext(files, tt, cb, i++)
60
      error('error', e)
61
    })
62
  }else {
63
    cb(i)
64
  }
65
}
66
67
function startProcess() {
68
  log('start publish all at path ' + processConfig.ABE_PATH)
69
  log('searching for file at ' + config.root)
70
  log('seach status: ' + processConfig.ABE_STATUS)
71
  log('save to: ' + path.join(config.root, processConfig.ABE_DESTINATION))
72
  var files = Manager.instance.getListWithStatusOnFolder(processConfig.ABE_STATUS, processConfig.ABE_PATH)
73
74
  log('Found ' + files.length + ' to republish')
75
76
  publishNext(files, files.length, function (i) {
77
    log('total ' + i + ' files')
78
    log('publish process finished ' + getTime())
79
    process.exit(0)
0 ignored issues
show
Compatibility Debugging Code Best Practice introduced by
Use of process.exit() is discouraged as it will potentially stop the complete node.js application. Consider quitting gracefully instead by throwing an Error.
Loading history...
80
  })
81
}
82
83
init('generate-posts',
84
  {
85
    ABE_STATUS: 'publish',
86
    ABE_PATH: '',
87
    ABE_DESTINATION: 'site'
88
  })
89
  .then(startProcess,
90
  (msg) => {
91
    error(msg)
92
    process.exit(0)
0 ignored issues
show
Compatibility Debugging Code Best Practice introduced by
Use of process.exit() is discouraged as it will potentially stop the complete node.js application. Consider quitting gracefully instead by throwing an Error.
Loading history...
93
  })